package cn.gok.web;

import cn.gok.service.NewStudentService;
import cn.gok.service.NewTeacherService;
import cn.gok.service.StudentService;
import cn.gok.service.impl.NewStudentServiceImpl;
import cn.gok.service.impl.NewTeacherServiceImpl;
import cn.gok.service.impl.StudentServiceImpl;
import cn.gok.util.Const;
import cn.gok.util.R;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.json.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();
    private NewStudentService newStudentService=new NewStudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        switch (action){
            case "login":login(req,resp);break;
            case "getVerifyCode":getVerifyCode(req,resp);break;

        }
    }
    //登录
    public void login(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //验证码验证
        String verificationCode = req.getParameter("verificationCode");
        String verifyCode = (String) req.getSession().getAttribute("verifyCode");
        if(!verificationCode.equals(verifyCode)){
            R r = R.fail(Const.VERIFY_CODE_FAIL,"验证码不正确,请重新输入");
            resp.getWriter().println(JSONUtil.toJsonStr(r));
            return;
        }

        String studentName = req.getParameter("studentName");
        String password = req.getParameter("password");

        R login = newStudentService.login(studentName, password);
        if ((int)login.get(R.CODE) == Const.SUCCESS_CODE){
            //存入session
            req.getSession().setAttribute("student", login.get("student"));
        }
        else if ((int)login.get(R.CODE) == Const.SUCCESS_CODE1){
            req.getSession().setAttribute("teacher",login.get("teacher"));
        }

//        R login1 = newTeacherService.login(studentName, password);
//        if ((int)login.get(R.CODE) == Const.SUCCESS_CODE1){
//            //存入session
//            req.getSession().setAttribute("teacher", login.get("teacher"));
//        }

//        R data = studentService.queryStudentByNameAndPassword(studentName, password);
//        if ((int)data.get(R.CODE) == Const.SUCCESS_CODE){
//            //存入session
//            req.getSession().setAttribute("student", data.get("student"));
//        }
//        else if ((int)data.get(R.CODE) == Const.SUCCESS_CODE1){
//            req.getSession().setAttribute("manager",data.get("manager"));
//        }

        String jsonStr = JSONUtil.toJsonStr(login);
        resp.getWriter().println(jsonStr);
    }
    /*
    本方法用于生成图形验证码
     */
    public void getVerifyCode(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(80, 40);

        //获取当前项目路径
        String contextPath = req.getSession().getServletContext().getRealPath("");

        //创建出验证码图片的存储路径
        File file = new File(contextPath+File.separator+"temp"+File.separator+"verifyCodeImage");

        //如果文件夹不存在，则创建
        if(file.exists()){
            file.mkdirs();
        }

        //生成验证码图片的名字
        String  verifyCodeName = UUID.randomUUID().toString()+".png";

        //生成验证码图片路径
        String verifyCodeImagePath = contextPath+File.separator+"temp"+File.separator+"verifyCodeImage"+File.separator+verifyCodeName;
        //图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(verifyCodeImagePath);

        //将验证码的值存入session
        req.getSession().setAttribute("verifyCode",lineCaptcha.getCode());

        //将验证码图片路径发送给前端
        R r = new R<>();
        r.put("verifyCodeName",verifyCodeName);

        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }


    public void forget(HttpServletRequest req,HttpServletResponse resp){
        //获取账号
        String studentName = req.getParameter("studentName");

    }
}